package com.example.springbootshiro.shiro.config;

import com.example.springbootshiro.util.ApplicationContextUtil;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;

import java.util.Collection;

/**
 * Created by YangGang on 2021/12/16 16:30。
 */
public class MyCacheManage {

    /**
     * 获取realm对象
     *
     * @return
     */
    public static AuthorizingRealm getRealm() {
        return ((AuthorizingRealm) ApplicationContextUtil.getBeanByBeanName("realm"));
    }

    /**
     * 【清空】-认证数据缓存
     */
    public static void clearAuthenticationCache() {
        getRealm().getAuthenticationCache().clear();
    }

    /**
     * 【清空】-授权数据缓存
     */
    public static void clearAuthorizationCache() {
        getRealm().getAuthorizationCache().clear();
    }

    /**
     * 移除-key对应的认证和授权缓存数据
     */
    public static void removeAuthCacheByKey(String key) {
        getRealm().getAuthenticationCache().remove(key);
        getRealm().getAuthorizationCache().remove(key);
    }

    /**
     * 查看-认证缓存数据
     */
    public static void getAuthenticationCache() {
        Collection<AuthenticationInfo> infos = getRealm().getAuthenticationCache().values();
        System.out.println(infos);
    }

    /**
     * 查看-授权缓存数据
     */
    public static void getAuthorizationCache() {
        Collection<AuthorizationInfo> infos = getRealm().getAuthorizationCache().values();
        System.out.println(infos);
    }

}
